package indi.jp.project.business.pub.service.impl;

import indi.jp.project.business.pub.dao.CompanyDAO;
import indi.jp.project.business.pub.pojo.po.CompanyPO;
import indi.jp.project.business.pub.pojo.query.CompanyQuery;
import indi.jp.project.core.entity.Datagrid;
import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.business.pub.service.CompanyService;
import indi.jp.project.core.util.SerialUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (PubCompany)表服务实现类
 *
 * @author makejava
 * @since 2021-08-16 08:43:34
 */
@Service
public class CompanyServiceImpl implements CompanyService {

    @Resource
    private CompanyDAO companyDAO;

    @Resource
    private SerialUtil serialUtil;

    @Override
    public JsonResult queryGrid(CompanyQuery query) throws Exception {
        List<CompanyPO> list = companyDAO.queryList(query);
        int count = companyDAO.queryCount(query);
        Datagrid<CompanyPO> datagrid = new Datagrid<>(list, count);
        return JsonResult.success(datagrid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult save(CompanyPO companyPO) throws Exception {
        if (StringUtils.isBlank(companyPO.getId())) {
            String id = String.valueOf(serialUtil.nextId());
            companyPO.setId(id);
            companyDAO.insert(companyPO);
        } else {
            companyDAO.update(companyPO);
        }
        return JsonResult.success("");
    }

    @Override
    public JsonResult deleteById(String id) throws Exception {
        companyDAO.deleteById(id);
        return JsonResult.success();
    }

    @Override
    public JsonResult findById(String id) throws Exception {
        CompanyPO companyPO = companyDAO.findById(id);
        return JsonResult.success(companyPO);
    }
}
